#include "Triangle.h"
#include "LinearFunction.h"
#include "Vector2D.h"
#include "Circle.h"

Circle::Circle(void)
{
}

Circle::~Circle(void)
{
}

Circle::Circle(const POINT & p1, const POINT & p2)//p1 mean the center of circle , p2 is on the circle 
{
  Vector2D v(p1,p2);
  this->m_radius = v.GetLength();
  this->m_center = p1;

}

bool Circle::SetOnTriangle(Triangle & tr)
{
  //Triangle has a three vectors
  //For each of them we have to find symetric function
  //and afterthat find a center of circle and radius
  int loop = 0;
  triangle_side  ts[3];
  LinearFunction lf_base[3];
  LinearFunction lf_sym[3];
  Vector2D  vector;
  POINT p[3];
  //CVector        vector;

  
  for(loop;loop < 3 ; loop++)
  {
    ts[loop] = tr.GetSide(loop);//bottom
  
    lf_base[loop].Create(ts[loop]._p1,ts[loop]._p2);
    
    vector.SetVector(ts[loop]._p1,ts[loop]._p2);

    lf_sym[loop].GetSymetricFunction(lf_base[loop],vector.GetCenterPoint());

  }

  //CVector v1(tr.m_bottom._p1,tr.m_bottom._p2);

  p[0] = lf_sym[0].GetCommonPoint(lf_sym[1]);
  p[1] = lf_sym[1].GetCommonPoint(lf_sym[2]);
  p[2] = lf_sym[2].GetCommonPoint(lf_sym[0]);

  //Now we have to check if all points are the same

  if((p[0].x == p[1].x && p[1].x == p[2].x && p[2].x == p[0].x) && (p[0].y == p[1].y && p[1].y == p[2].y && p[2].y == p[0].y))
  {
    this->m_center = p[0];
  }

  //Now calculate the radius 
  vector.SetVector(m_center,ts[0]._p1);
  this->m_radius = vector.GetLength();
  
  return true;
}

bool Circle::IsPointInside(const POINT & p)
{
  //The point is inside circle if fulfill the following quotation:
  //r^2 >= x^2 + y^2 
  //the radius is known, because the triangle is set
  //but we have to find a difference between center of circle and given point
  //and calculate the radius
  //so it is like:
  // (m_center.x - point.x)^2 and (m_center.y - p.y)^2

  double point_radius;

  point_radius = pow((double)(m_center.x - p.x),2) + pow((double)(m_center.y - p.y),2);

  point_radius = sqrt(point_radius); 
  //now we have a calculated radius based on the point
  //And we can check if the calculated radius based on the given point 
  //is less or equal the cicrcle radius
  if(this->m_radius >= point_radius)
    return true;
  else
    return false;
}